//
//  HHLMainTabBarVC.swift
//  SwiftWeibo
//
//  Created by 呆萌诺 on 15/10/26.
//  Copyright © 2015年 呆萌诺. All rights reserved.
//

import UIKit

class HHLMainTabBarVC: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
//        tabBar.setValue(<#T##value: AnyObject?##AnyObject?#>, forKey: "name")
        
        //设置全局tabBar颜色
        tabBar.tintColor = UIColor.orangeColor()
        
        let homeVC = HHLHomeVC()
        self.addChildViewController(homeVC, title: "首页", imgName: "tabbar_home")
        
        let disVC = HHLDiscoverVC()
        self.addChildViewController(disVC, title: "发现", imgName: "tabbar_discover")
        
        //添加加号按钮 CUICatalog: Invalid asset name supplied:（原因，imgName不能为空）
        let plusButtonVC = UIViewController()
        self.addChildViewController(plusButtonVC, title: "", imgName: "j")
        /*
        注意在这创建的按钮在tabBar下面
        */
        
        
       
        let messageVC = HHLMessageVC()
        self.addChildViewController(messageVC, title: "消息", imgName: "tabbar_message_center")
        
        let profileVC = HHLProfileVC()
        self.addChildViewController(profileVC, title: "我", imgName: "tabbar_profile")
        
        let plusBtnW = tabBar.bounds.width/5
        let plusBtnH = tabBar.bounds.height
        plusBtn.frame = CGRect(x: plusBtnW * 2, y: 0, width: plusBtnW, height: plusBtnH)
        tabBar.addSubview(plusBtn)
    }
    
    //创建按钮
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
//        let plusBtnW = tabBar.bounds.width/5
//        let plusBtnH = tabBar.bounds.height
//        plusBtn.frame = CGRect(x: plusBtnW * 2, y: 0, width: plusBtnW, height: plusBtnH)
//        tabBar.addSubview(plusBtn)
        
    }
    
    func buttonAction() {
        print(__FUNCTION__)
    }
    
    
    
    ///添加子控制器
    func addChildViewController(controller : UIViewController, title : String ,imgName : String ) {
        
        controller.title = title
        controller.tabBarItem.image = UIImage(named: imgName)
        let nav = UINavigationController(rootViewController: controller)
        addChildViewController(nav)
        }
    
    //MARK: - Setter & Getter
    lazy var plusBtn : UIButton = {
        let button = UIButton()
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        button.addTarget(self, action: "buttonAction", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()

}





//private func addChildViewController(controller: UIViewController, title: String, imageName: String) {
//    controller.title = title
//    controller.tabBarItem.image = UIImage(named: imageName)
//    addChildViewController(UINavigationController(rootViewController: controller))
//}